package org.layuiframework.generator.config;

import org.layuiframework.generator.annotation.DbType;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataSourceConfig {
//    private IDbQuery dbQuery;
    private DbType dbType;
    private String schemaName;
//    private ITypeConvert typeConvert;
//    private IKeyWordsHandler keyWordsHandler;
    private String url;
    private String driverName;
    private String username;
    private String password;

//    public IDbQuery getDbQuery() {
//        if (null == this.dbQuery) {
//            DbType dbType = this.getDbType();
//            DbQueryRegistry dbQueryRegistry = new DbQueryRegistry();
//            this.dbQuery = (IDbQuery)Optional.ofNullable(dbQueryRegistry.getDbQuery(dbType)).orElseGet(() -> {
//                return dbQueryRegistry.getDbQuery(DbType.MYSQL);
//            });
//        }
//
//        return this.dbQuery;
//    }

    public DbType getDbType() {
        if (null == this.dbType) {
            this.dbType = this.getDbType(this.url.toLowerCase());
            if (null == this.dbType) {
//                throw ExceptionUtils.mpe("Unknown type of database!", new Object[0]);
                throw new RuntimeException();
            }
        }
        return this.dbType;
    }

    private DbType getDbType(String str) {
        if (!str.contains(":mysql:") && !str.contains(":cobar:")) {
            if (str.contains(":postgresql:")) {
                return DbType.POSTGRE_SQL;
            } else {
                return DbType.OTHER;
            }
        } else {
            return DbType.MYSQL;
        }
    }

//    public ITypeConvert getTypeConvert() {
//        if (null == this.typeConvert) {
//            DbType dbType = this.getDbType();
//            this.typeConvert = TypeConverts.getTypeConvert(dbType);
//            if (null == this.typeConvert) {
//                this.typeConvert = MySqlTypeConvert.INSTANCE;
//            }
//        }
//
//        return this.typeConvert;
//    }

    public Connection getConn() {
        try {
            Class.forName(this.driverName);
            Connection conn = DriverManager.getConnection(this.url, this.username, this.password);
            return conn;
        } catch (SQLException | ClassNotFoundException var3) {
            throw new RuntimeException(var3);
        }
    }

    public DataSourceConfig() {
    }

    public String getSchemaName() {
        return this.schemaName;
    }

//    public IKeyWordsHandler getKeyWordsHandler() {
//        return this.keyWordsHandler;
//    }

    public String getUrl() {
        return this.url;
    }

    public String getDriverName() {
        return this.driverName;
    }

    public String getUsername() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }

//    public DataSourceConfig setDbQuery(final IDbQuery dbQuery) {
//        this.dbQuery = dbQuery;
//        return this;
//    }

    public DataSourceConfig setDbType(final DbType dbType) {
        this.dbType = dbType;
        return this;
    }

    public DataSourceConfig setSchemaName(final String schemaName) {
        this.schemaName = schemaName;
        return this;
    }

//    public DataSourceConfig setTypeConvert(final ITypeConvert typeConvert) {
//        this.typeConvert = typeConvert;
//        return this;
//    }

//    public DataSourceConfig setKeyWordsHandler(final IKeyWordsHandler keyWordsHandler) {
//        this.keyWordsHandler = keyWordsHandler;
//        return this;
//    }

    public DataSourceConfig setUrl(final String url) {
        this.url = url;
        return this;
    }

    public DataSourceConfig setDriverName(final String driverName) {
        this.driverName = driverName;
        return this;
    }

    public DataSourceConfig setUsername(final String username) {
        this.username = username;
        return this;
    }

    public DataSourceConfig setPassword(final String password) {
        this.password = password;
        return this;
    }

}
